package com.example.stars_project_android.index;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.stars_project_android.R;
import com.example.stars_project_android.util.Constant;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMCmdMessageBody;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.EaseIM;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.modules.chat.EaseChatFragment;
import com.hyphenate.easeui.modules.chat.EaseChatInputMenu;
import com.hyphenate.easeui.modules.chat.EaseChatLayout;
import com.hyphenate.easeui.modules.chat.EaseChatMessageListLayout;
import com.hyphenate.easeui.modules.chat.interfaces.IChatEmojiconMenu;
import com.hyphenate.easeui.modules.chat.interfaces.IChatExtendMenu;
import com.hyphenate.easeui.modules.chat.interfaces.IChatPrimaryMenu;
import com.hyphenate.easeui.modules.chat.interfaces.OnChatLayoutListener;
import com.hyphenate.easeui.provider.EaseUserProfileProvider;
import com.hyphenate.easeui.widget.EaseTitleBar;

import java.util.List;

/**
 * 房间详情页面
 */
public class RoomDetail extends AppCompatActivity {

    // 聊天信息输入框
    private EditText mInputEdit;

    // 发送按钮
    private Button mSendBtn;

    // 显示内容的 TextView
    private TextView mContentText;

    // 消息监听器
    private EMMessageListener mMessageListener;
    // 当前聊天的 ID
    private String mChatId;
    // 当前会话对象
    private EMConversation mConversation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_detail);

        String chatRoomId = getIntent().getStringExtra("EaseConstant.EXTRA_CONVERSATION_ID");
        EaseChatFragment chatFragment = new EaseChatFragment();
        chatFragment.setArguments(getIntent().getExtras());
        EaseIM.getInstance().setUserProvider(new EaseUserProfileProvider() {
            @Override
            public EaseUser getUser(String username) {
                //根据 username，从数据库中或者内存中取出之前保存的用户信息，如从数据库中取出的用户对象为 DemoUserBean
                EaseUser user = new EaseUser(username);
                //设置用户昵称
                user.setNickname(Constant.starName);
                chatFragment.setOnChatLayoutListener(new OnChatLayoutListener() {
                    /**
                     * 点击消息 bubble 区域
                     * @param message
                     * @return
                     */
                    @Override
                    public boolean onBubbleClick(EMMessage message) {
                        return false;
                    }
                    /**
                     * 长按消息 bubble 区域
                     * @param v
                     * @param message
                     * @return
                     */
                    @Override
                    public boolean onBubbleLongClick(View v, EMMessage message) {
                        return false;
                    }
                    /**
                     * 点击头像
                     * @param username
                     */
                    @Override
                    public void onUserAvatarClick(String username) {
                        Intent intent = new Intent(RoomDetail.this, SelfChat.class);
                        intent.putExtra(EaseConstant.EXTRA_CONVERSATION_ID,"18329390365");
                        intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EMMessage.ChatType.Chat);
                        startActivity(intent);
                    }
                    /**
                     * 长按头像
                     * @param username
                     */
                    @Override
                    public void onUserAvatarLongClick(String username) {

                    }
                    /**
                     * 条目点击
                     * @param view
                     * @param itemId
                     */
                    @Override
                    public void onChatExtendMenuItemClick(View view, int itemId) {

                    }
                    /**
                     * EditText 文本变化监听
                     * @param s
                     * @param start
                     * @param before
                     * @param count
                     */
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }
                    /**
                     * 聊天中错误信息
                     * @param code
                     * @param errorMsg
                     */
                    @Override
                    public void onChatError(int code, String errorMsg) {

                    }
                });

                //设置头像地址
                user.setAvatar("android.resource://" + getPackageName() + "/" + R.mipmap.user_img1);
                //最后返回构建的 EaseUser 对象
                return user;
            }
        });
        getSupportFragmentManager().beginTransaction().add(R.id.ec_layout_container, chatFragment).commit();
        EaseTitleBar titleBarMessage = findViewById(R.id.title_bar);
        //设置标题
        titleBarMessage.setTitle("聊天室");
        //设置标题位置
        titleBarMessage.setTitlePosition(EaseTitleBar.TitlePosition.Center);
        //设置返回按钮的点击事件
        titleBarMessage.setOnBackPressListener(new EaseTitleBar.OnBackPressListener() {
            @Override
            public void onBackPress(View view) {
                EMClient.getInstance().chatroomManager().leaveChatRoom(chatRoomId);
                finish();
            }
        });

    }
}